package com.caj.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class RemoteService {

    //模拟生成订单接口
    public JSONObject createOrder(long userId) {
        try {
            Thread.sleep(1);//每个接口   消耗1毫秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JSONObject result = new JSONObject();
        double random = Math.random()*1000;
        long orderId =(long) random;
        result.put("userId", 10086);
        result.put("orderId", orderId);
        result.put("goodsId", 13);
        result.put("goodsNum", 2);
        result.put("points", 100);
        result.put("deliverId", 8848);
        return result;
    }
    //处理库存接口（单个）
    public JSONObject dealGoods( JSONObject object) {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        object.put("dealGoods", "ok");
        return object;
    }
    //处理积分接口
    public JSONObject dealPoints( JSONObject object) {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        object.put("dealPoints", "ok");
        return object;
    }
    //处理物流接口
    public JSONObject dealDeliver( JSONObject object) {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        object.put("dealDeliver", "ok");
        return object;
    }

    //模拟生成订单接口（无延时）
    public JSONObject createOrderFast(long userId) {
        JSONObject result = new JSONObject();
        double random = Math.random()*1000;
        long orderId =(long) random;
        result.put("userId", 10086);
        result.put("orderId", orderId);
        result.put("goodsId", 13);
        result.put("goodsNum", 2);
        result.put("points", 100);
        result.put("deliverId", 8848);
        return result;
    }
    //处理库存信息 （批量接口）  1000,
    public List<JSONObject> dealGoodsFastBatch(List<JSONObject> objectList) {
        List<JSONObject> list = objectList;
        Iterator it = list.iterator();
        while(it.hasNext()){
            JSONObject result =(JSONObject)it.next();
            result.put("dealGoods", "ok");
        }
        return  list;
    }
}
